package com.zbdx.dao.impl;

import com.zbdx.dao.IOrderDao;
import com.zbdx.domain.Order;
import com.zbdx.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;

/**
 * TODO  用户数据访问层实现类
 *
 * @author shengyuee
 * @version 1.0
 * 2024/9/10 15:03
 */

public class OrderDaoImpl implements IOrderDao {
    private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public int deleteById(int id) throws SQLException {
        String sql = "DELETE FROM t_order WHERE orderId=?";
        int res = qr.update(sql, id);
        return res;
    }

    /**
     * 根据Id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public Order selectById(int id) throws SQLException {
        String sql = "SELECT * FROM t_order WHERE orderId=?";
        Order order = qr.query(sql, new BeanHandler<Order>(Order.class),id);
        return order;

    }
    @Override
    public int insert(Order order) throws SQLException {
        String sql = "INSERT INTO t_user VALUES(?,?,?,?,?,?,?)";
        Object[] param = {order.getOrderId(),order.getOrderName(),order.getOrderPrice(),order.getOrderTime(),order.getOrder_image_url(),order.getOrderStatus(),order.getOrderType()};
        int res = qr.update(sql, param);
        return res;
    }
    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Order> selectAll() throws SQLException {
        String sql = "SELECT * FROM t_order";
        // 调用QueryRunner的query方法查询
        List<Order> list = qr.query(sql, new BeanListHandler<Order>(Order.class));
        return list;
    }
}
